﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using ViewInterfaces;
using Microsoft.SharePoint.WebControls;
using System.Data;
using Presenter;
using Model;


namespace CSCV.Group1.NewsAnnouncement.ListNews
{
    public partial class ListNewsUserControl : UserControl, INewsView
    {
        public DataTable dt
        {
            get 
            {
                return  ViewState["ListNews_DataSource"] as DataTable;
            }

            set
            {
                ViewState["ListNews_DataSource"] = value;
            }
        }

        public int numTop
        {
            get;
            set;
        }
        
        public bool showTop;

        ShowNewsBasePresenter presenter;
        NewsDAO newsDao;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                newsDao = new NewsDAO();

                if (showTop)
                {
                    presenter = new ShowTop4NewsPresenter(this, newsDao);
                    switchToShowTop();
                }
                else
                {
                    presenter = new ShowAllNewsPresenter(this, newsDao);
                    switchToShowAll();
                }
                presenter.Init();
            }

            BindDatatToGrid();
            spGridView.PagerTemplate = null;
            spGridView.PageIndexChanging += new GridViewPageEventHandler(spGridView_PageIndexChanging);

        }

        private void BindDatatToGrid()
        {
            spGridView.DataSource = dt;
            spGridView.DataBind();
        }

        private void switchToShowAll()
        {
            // config paging function
            spGridView.AllowPaging = true;
            spGridView.PageSize = numTop;
            spGridView.PagerSettings.Position = PagerPosition.Bottom;
            spGridView.PagerTemplate = null;
        }

        private void switchToShowTop()
        {
            // config paging function
            spGridView.AllowPaging = false; 
        }

        protected void spGridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            spGridView.PageIndex = e.NewPageIndex;
            BindDatatToGrid();
        }

    }
}